package cn.tedu.onlinestore.order.service.impl;

import cn.tedu.onlinestore.address.mapper.AddressMapper;
import cn.tedu.onlinestore.address.pojo.vo.AddressVO;
import cn.tedu.onlinestore.cart.pojo.vo.CartVO;
import cn.tedu.onlinestore.cart.service.CartService;
import cn.tedu.onlinestore.order.mapper.OrderMapper;
import cn.tedu.onlinestore.order.pojo.dto.OrderParam;
import cn.tedu.onlinestore.order.pojo.entity.Order;
import cn.tedu.onlinestore.order.pojo.vo.OrderVO;
import cn.tedu.onlinestore.order.service.OrderService;
import cn.tedu.onlinestore.product.pojo.dto.ProductParam;
import cn.tedu.onlinestore.product.pojo.vo.ProductVO;
import cn.tedu.onlinestore.product.service.ProductService;
import cn.tedu.onlinestore.user.mapper.UserMapper;
import cn.tedu.onlinestore.user.pojo.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    CartService cartService;
    @Autowired
    ProductService productService;
    @Autowired
    UserMapper userMapper;
    @Autowired
    AddressMapper addressMapper;
    @Override
    public void create(OrderParam orderParam) {
        log.debug("开始处理创建订单请求业务:{}",orderParam);
        List<CartVO> cartVOS = cartService.cartList(orderParam.getUserId());
        for (CartVO cartVO : cartVOS) {
            log.debug("购物车信息：{}",cartVO);
            Order order = new Order();
            BeanUtils.copyProperties(cartVO,order);

            ProductVO productVO = productService.selectProductById(order.getProductsId());
            order.setSellerId(productVO.getSellerId());
            order.setStatus("1");
            order.setCreateTime(new Date());
            order.setAddressId(orderParam.getAddressId());
            order.setTotal(orderParam.getTotal());
            log.debug("订单信息：{}",order);
            orderMapper.insertOrder(order);

            ProductParam productParam = new ProductParam();
            productParam.setId(order.getProductsId());
            productParam.setNumber(productVO.getNumber()-order.getAmount());
            log.debug("库存减少：{}",productParam.getNumber());
            productService.updateProduct(productParam);
        }
        cartService.deleteByUserId(orderParam.getUserId());
    }

    @Override
    public List<OrderVO> list(OrderParam orderParam) {
        log.debug("开始处理查询订单请求业务:{}",orderParam);
        List<OrderVO> list = new ArrayList<>();
        List<Order> orderList = orderMapper.selectOrderById(orderParam);
        for (Order order : orderList) {
            OrderVO orderVO = new OrderVO();
            UserVO userVO = userMapper.selectUserById(order.getUserId());
            orderVO.setUsername(userVO.getUsername());
            AddressVO addressVO = addressMapper.addressById(order.getAddressId());
            orderVO.setAddress(addressVO.getAddress());
            ProductVO productVO = productService.selectProductById(order.getProductsId());
            orderVO.setName(productVO.getName());
            orderVO.setAmount(order.getAmount());
            orderVO.setTotal(order.getTotal());
            orderVO.setCreateTime(order.getCreateTime());
            orderVO.setId(order.getId());
            orderVO.setStatus(order.getStatus());
            list.add(orderVO);


        }
        return list;
    }

    @Override
    public void cancel(OrderParam orderParam) {
        log.debug("开始处理取消订单请求业务:{}",orderParam);
        Order order = new Order();
        BeanUtils.copyProperties(orderParam,order);
        orderMapper.updateOrder(order);
    }
}
